package org.web.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.net.util.AnalysisContentUtil;
import org.net.util.FetchUtil;
import org.net.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web.core.Page;
import org.web.core.service.ResourceService;
import org.web.entity.Resources;

/**
 * 更新source的外链接数和域名注册时间
 * @Author : yingx
 * @CreateTime : 2011-10-10 下午01:51:51
 * @Pageage : org.web.schedule
 */
public class CallAutoExecuteSiteInfoJob {
	
	@Resource(name = "resourceService")
    private ResourceService resourceService;
	
	private static Logger log = LoggerFactory.getLogger(CallAutoExecuteSiteInfoJob.class);
    
    @SuppressWarnings("unchecked")
	public void execute() {
    	log.info("quartz schedule is starting.....");
    	Map<String, Object> parameters =new HashMap();
    	parameters.put("resolveSource","yes");
    	Page resource = resourceService.findByPage(new Page(1,50), parameters);
    	List<Resources> sources = resource.getResult();
    	SimpleDateFormat dateformat=new SimpleDateFormat("dd-MM-yyyy");
    	for(Resources rs : sources){
    	 try{
         String inlink = "";
         String creation ="";
   		 String targetUrl ="http://siteexplorer.search.yahoo.com/search?p=www."+AnalysisContentUtil.getItsDomainName(rs.getUrl())+"&fr=sfp&bwm=i";
   		 String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
   		 if(!StringUtil.isEmpty(responseBody)){
   			Pattern p = null;
   			Matcher m = null;
   			StringBuffer sb0 = new StringBuffer();
   			String regex ="</li><li><span class=\"btn\">Inlinks(.*)</span></li></ul></div><div id=\"inlink-options\" class=\"opt-list opt-right\"><form method=\"get\" action=\"http://siteexplorer.search.yahoo.com/search\"><label for=\"bwmo\" class=\"optdesc\">Show Inlinks:</label>";
   			p = Pattern.compile(regex);
   			m = p.matcher(responseBody);
   			if (m.find()) {
   				sb0.append(m.group(1));
   			}
   			if(!StringUtil.isEmpty(sb0.toString())){
   			inlink = sb0.toString().replaceAll("\\,", "").replaceAll("\\(", "").replaceAll("\\)","").trim();
   			}
   		 }
   		 
   		 targetUrl ="http://www.whois-search.com/whois/"+AnalysisContentUtil.getItsDomainName(rs.getUrl());
   		 responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
   		 if(!StringUtil.isEmpty(responseBody)){
   			Pattern p = null;
   			Matcher m = null;
   			StringBuffer sb0 = new StringBuffer();
   			String regex ="Creation Date:(.*)";
   			p = Pattern.compile(regex);
   			m = p.matcher(responseBody);
   			while (m.find()) {
   				sb0.append(m.group(1));
   			}
   			if(!StringUtil.isEmpty(sb0.toString())){
   			creation = sb0.toString().replace("jan","01").replace("feb", "02").replace("mar", "03").replace("apr", "04").replace("may", "05")
   			.replace("jun", "06").replace("jul", "07").replace("aug", "08").replace("sep", "09").replace("oct", "10").replace("nov", "11").replace("dec", "12");
   			}
    	 }
   		 if(!StringUtil.isEmpty(inlink) && !StringUtil.isEmpty(creation)){
   			rs.setLinkCount(Long.parseLong(inlink));
   			Date create = dateformat.parse(creation);
   			rs.setDomainCreationDate(create);
   			resourceService.update(rs);
   		 }else{
   			 rs.setIsCrawled("Y");   //将不再出现在抓取列表中,视无效资源
   			 resourceService.update(rs);
   		 }
    	 }
   		 catch(Exception e) {
   			 e.printStackTrace();
   			 rs.setIsCrawled("E");   //将不再出现在抓取列表中,视无效资源
  			 resourceService.update(rs);
   			 continue;
   		 }
    	}
    	log.info("quartz schedule is end");
    }
}
